home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / AZKVA0 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  5.0 KB  |  120 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import java.util.Vector;
  4.  
  5. public class PlainDocument extends AbstractDocument {
  6.    public static final String tabSizeAttribute = "tabSize";
  7.    public static final String lineLimitAttribute = "lineLimit";
  8.    private AbstractDocument.AbstractElement defaultRoot;
  9.    private Vector added;
  10.    private Vector removed;
  11.  
  12.    public PlainDocument() {
  13.       this(new StringContent());
  14.    }
  15.  
  16.    protected PlainDocument(AbstractDocument.Content c) {
  17.       super(c);
  18.       this.added = new Vector();
  19.       this.removed = new Vector();
  20.       ((AbstractDocument)this).putProperty("tabSize", new Integer(8));
  21.       this.defaultRoot = this.createDefaultRoot();
  22.    }
  23.  
  24.    protected AbstractDocument.AbstractElement createDefaultRoot() {
  25.       AbstractDocument.BranchElement map = new AbstractDocument.BranchElement(this, (Element)null, (AttributeSet)null);
  26.       AbstractDocument.LeafElement line = new AbstractDocument.LeafElement(this, map, (AttributeSet)null, 0, 1);
  27.       Element[] lines = new Element[1];
  28.       lines[0] = line;
  29.       map.replace(0, 0, lines);
  30.       return map;
  31.    }
  32.  
  33.    public Element getDefaultRootElement() {
  34.       return this.defaultRoot;
  35.    }
  36.  
  37.    protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr) {
  38.       this.removed.removeAllElements();
  39.       this.added.removeAllElements();
  40.       AbstractDocument.BranchElement lineMap = (AbstractDocument.BranchElement)this.getDefaultRootElement();
  41.       int offset = chng.getOffset();
  42.       int length = chng.getLength();
  43.       if (offset > 0) {
  44.          --offset;
  45.          ++length;
  46.       }
  47.  
  48.       int index = lineMap.getElementIndex(offset);
  49.       Element rmCandidate = lineMap.getElement(index);
  50.       int rmOffs0 = rmCandidate.getStartOffset();
  51.       int rmOffs1 = rmCandidate.getEndOffset();
  52.       int lastOffset = rmOffs0;
  53.  
  54.       try {
  55.          String str = ((AbstractDocument)this).getText(offset, length);
  56.          boolean hasBreaks = false;
  57.  
  58.          for(int i = 0; i < length; ++i) {
  59.             char c = str.charAt(i);
  60.             if (c == '\n') {
  61.                int breakOffset = offset + i + 1;
  62.                this.added.addElement(new AbstractDocument.LeafElement(this, lineMap, (AttributeSet)null, lastOffset, breakOffset));
  63.                lastOffset = breakOffset;
  64.                hasBreaks = true;
  65.             }
  66.          }
  67.  
  68.          if (hasBreaks) {
  69.             int rmCount = 1;
  70.             this.removed.addElement(rmCandidate);
  71.             if (offset + length == rmOffs1 && lastOffset != rmOffs1 && index + 1 < lineMap.getElementCount()) {
  72.                ++rmCount;
  73.                Element e = lineMap.getElement(index + 1);
  74.                this.removed.addElement(e);
  75.                rmOffs1 = e.getEndOffset();
  76.             }
  77.  
  78.             if (lastOffset < rmOffs1) {
  79.                this.added.addElement(new AbstractDocument.LeafElement(this, lineMap, (AttributeSet)null, lastOffset, rmOffs1));
  80.             }
  81.  
  82.             Element[] aelems = new Element[this.added.size()];
  83.             this.added.copyInto(aelems);
  84.             Element[] relems = new Element[this.removed.size()];
  85.             this.removed.copyInto(relems);
  86.             AbstractDocument.ElementEdit ee = new AbstractDocument.ElementEdit(lineMap, index, relems, aelems);
  87.             chng.addEdit(ee);
  88.             lineMap.replace(index, relems.length, aelems);
  89.          }
  90.  
  91.       } catch (BadLocationException var17) {
  92.          throw new Error("Internal error: " + ((Throwable)var17).toString());
  93.       }
  94.    }
  95.  
  96.    protected void removeUpdate(AbstractDocument.DefaultDocumentEvent chng) {
  97.       this.removed.removeAllElements();
  98.       AbstractDocument.BranchElement map = (AbstractDocument.BranchElement)this.getDefaultRootElement();
  99.       int offset = chng.getOffset();
  100.       int length = chng.getLength();
  101.       int line0 = map.getElementIndex(offset);
  102.       int line1 = map.getElementIndex(offset + length);
  103.       if (line0 != line1) {
  104.          for(int i = line0; i <= line1; ++i) {
  105.             this.removed.addElement(map.getElement(i));
  106.          }
  107.  
  108.          int p0 = map.getElement(line0).getStartOffset();
  109.          int p1 = map.getElement(line1).getEndOffset();
  110.          Element[] aelems = new Element[]{new AbstractDocument.LeafElement(this, map, (AttributeSet)null, p0, p1)};
  111.          Element[] relems = new Element[this.removed.size()];
  112.          this.removed.copyInto(relems);
  113.          AbstractDocument.ElementEdit ee = new AbstractDocument.ElementEdit(map, line0, relems, aelems);
  114.          chng.addEdit(ee);
  115.          map.replace(line0, relems.length, aelems);
  116.       }
  117.  
  118.    }
  119. }
  120.